package org.xxs.inanword.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xxs.inanword.common.AnWord;
import org.xxs.inanword.service.AnWordService;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/inanword")
@CrossOrigin(origins = "*")
public class InAnWordController {

    @Autowired
    private AnWordService anWordService;
    private String key = "e29652d832cad3f4932ca02136a796b2";

    @RequestMapping("/getAllData")
    public Map<String, Object> getAllData() {
        Map<String, Object> anWord = new HashMap<String, Object>();
        // 获取当前日期和时间
        LocalDateTime now = LocalDateTime.now();
        // 创建日期格式化器
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 格式化当前日期
        String formattedDate = now.format(formatter);

        AnWord aw = anWordService.getOne(
                new QueryWrapper<AnWord>().eq("time", formattedDate)
        );

        if (aw != null) {
            anWord = stringToMap(aw.getWord());
        } else {

            Map<String, Object> qiaomen = mapResult("https://apis.tianapi.com/qiaomen/index?key=", key);
            Map<String, Object> dujitang = mapResult("https://apis.tianapi.com/dujitang/index?key=", key);
            Map<String, Object> proverb = mapResult("https://apis.tianapi.com/proverb/index?key=", key);
            Map<String, Object> dgrl = mapResult("https://apis.tianapi.com/dgryl/index?key=", key);
            Map<String, Object> caihongpi = mapResult("https://apis.tianapi.com/caihongpi/index?key=", key);
            Map<String, Object> dailyEng = mapResult("https://api.vvhan.com/api/dailyEnglish", "");

            anWord.put("qiaomen", qiaomen);
            anWord.put("dujitang", dujitang);
            anWord.put("proverb", proverb);
            anWord.put("dgrl", dgrl);
            anWord.put("caihongpi", caihongpi);
            anWord.put("dailyEng", dailyEng);

            try {
                String json = new ObjectMapper().writeValueAsString(anWord);
                anWordService.save(new AnWord(formattedDate, json));
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }

        }


        return anWord;

    }

    public Map<String, Object> mapResult(String url, String key) {
        Map<String, Object> map = new HashMap<>();
        Mono<String> stringMono = anWordService.sendRequest(url + key);
        String block = stringMono.block();
        map = stringToMap(block);
        return map;
    }

    ;


    public Map<String, Object> stringToMap(String content) {
        Map<String, Object> map = new HashMap<>();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            map = objectMapper.readValue(content, map.getClass());
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        return map;
    }


}
